/**
 * 
 */
package datatype;

import java.util.ArrayList;

/**
 * @author Artur Tolstenco
 * 
 */

/**
 * Class that implements the Vertex data structure.
 * A vertex is identified by:
 * 		a name;
 * 		a list of nodes that can be reached from the vertex
 * 		a index that is a unique identifier
 * 
 */
public class Vertex {
	private String name;
	private ArrayList<Edge> adjacents;
	
	private int index;

	public Vertex(String name) {
		this.name = name;
		adjacents = new ArrayList<Edge>();
	}
	
	protected void setIndex(int index) {
		this.index = index;
	}
	
	public int getIndex() {
		return this.index;
	}

	/**
	 * Given an edge, adds it to the adjacents
	 * @param edge to add to adjacents
	 * @throws NegativeWeightEdgeException if weight of edge is negative
	 * @throws AlreadyExistigEdgeException if the edge already exists in adjacents
	 */
	protected void addAdjacent(Edge edge) throws NegativeWeightEdgeException,
			AlreadyExistigEdgeException {
		if (Double.compare(edge.getWeight(), 0.0d) < 0)
			throw new NegativeWeightEdgeException();
		if (adjacents.contains(edge))
			throw new AlreadyExistigEdgeException();

		adjacents.add(edge);
	}

	protected ArrayList<Edge> adjacents() {
		return adjacents;
	}

	
	@Override
	public String toString() {
		return name;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Vertex other = (Vertex) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

}
